package com.masterclient.app.modules.search;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.search.adapter.SearchShopRecyclerAdapter;
import com.masterclient.app.modules.search.contract.ShopResultContract;
import com.masterclient.app.modules.search.contract.ShopResultPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/6/23 17:56
 * 功能说明：搜索店铺结果
 */

public class SearchShopResultFragment extends BaseFragment<ShopResultPresenter> implements ShopResultContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    private SearchShopRecyclerAdapter searchShopRecyclerAdapter;
    private List<SearchShopMessage> shopMessageList;
    private int totalRow;
    String keyword;
    boolean first;
    View emptyView;

    int page = 1;
    int page_size = 20;

    private boolean hasMore;

    public static SearchShopResultFragment getInstance(@NonNull String keyword) {
        Bundle bundle = new Bundle();
        bundle.putString("keyword",keyword);
        SearchShopResultFragment searchShopResultFragment = new SearchShopResultFragment();
        searchShopResultFragment.setArguments(bundle);
        return searchShopResultFragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_search_shop_result, null);
    }

    @Override
    protected void initOnlyOnce() {

        Bundle bundle = getArguments();
        keyword = bundle.getString("keyword");

        searchShopRecyclerAdapter = new SearchShopRecyclerAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(searchShopRecyclerAdapter);
        searchShopRecyclerAdapter.bindToRecyclerView(recyclerView);
        emptyView = View.inflate(mContext,R.layout.null_search,null);
        ((TextView)emptyView.findViewById(R.id.tv_tip)).setText("很抱歉，还没有相关的内容");
        searchShopRecyclerAdapter.setEmptyView(emptyView);
        searchShopRecyclerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SearchShopMessage sellerMessage = shopMessageList.get(position);
                ARouter.getInstance().build(AroutePath.STORE_ACTIVITY).withString("store_id", sellerMessage.getSh_id()).navigation();
            }
        });

        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                if(shopMessageList != null){
                    shopMessageList.clear();
                }
                mPresenter.searchShopList(keyword,"" + page,"" + page_size);
                if(searchShopRecyclerAdapter != null){
                    searchShopRecyclerAdapter.setEnableLoadMore(true);
                }
            }
        });

        searchShopRecyclerAdapter.setOnLoadMoreListener(()->{
            if(hasMore){
                page++;
                mPresenter.searchShopList(keyword,"" + page,"" + page_size);
            }
        },recyclerView);
    }

    @Override
    protected void initView(View mRootView) {
        mPresenter.searchShopList(keyword,"" + page,"" + page_size);
        super.initView(mRootView);
    }

    public void setNewKeyWord(String keyword){
        first = true;
        page = 1;
        this.keyword = keyword;
    }

    public void searchNewKeyWord(String keyword){
        page = 1;
        this.keyword = keyword;
        if(shopMessageList != null){
            shopMessageList.clear();
        }
        mPresenter.searchShopList(keyword,"" + page,"" + page_size);
        if(searchShopRecyclerAdapter != null){
            searchShopRecyclerAdapter.setEnableLoadMore(true);
        }
    }


    @Override
    public void showSearchBrand(List<SearchBrandBean.DataBean> dataBean) {

    }

    @Override
    public void showSearchShop(SearchShopData data) {
        first = false;

        if(swipeLayout!=null&&swipeLayout.isRefreshing()){
            swipeLayout.setRefreshing(false);
        }

        hasMore =  !(data == null||data.getSh_items()==null||
                data.getSh_items().size()<=0|| !data.isSh_has_more()) ;

        if(shopMessageList == null){
            shopMessageList = new ArrayList<>();
        }
        if(page == 1){
            shopMessageList.clear();
        }
        if(data != null){
            shopMessageList.addAll(data.getSh_items());
        }
        searchShopRecyclerAdapter.setNewData(shopMessageList);
        if(shopMessageList.size()<=0){
            emptyView.setVisibility(View.VISIBLE);
        }

        if(!hasMore){
            searchShopRecyclerAdapter.setEnableLoadMore(false);
        }

//        if(swipeLayout.isRefreshing()){
//            swipeLayout.setRefreshing(false);
//        }
//        if(shopMessageList == null){
//            shopMessageList = new ArrayList<>();
//        }
//        emptyView.setVisibility(View.VISIBLE);
//        shopMessageList = data.getSh_items();
//        searchShopRecyclerAdapter.setNewData(shopMessageList);
    }

    @Override
    public void loadFailed() {
        if(swipeLayout!=null){
            swipeLayout.setRefreshing(false);
        }
        searchShopRecyclerAdapter.setEnableLoadMore(false);
        emptyView.setVisibility(View.VISIBLE);
    }



}
